﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using Yottamud_Project_V3;
using Yottamud_2013_v3_Main.Game_Files;
using System.Collections.ObjectModel;

namespace Yottamud_2013_v3_Main
{
    /// <summary>
    /// Interaction logic for SystemSettingsWindow.xaml
    /// </summary>
    public partial class SystemSettingsWindow : Window
    {

        private SystemSettings _systemSettingManager;
        private string _systemSettingFileName = string.Empty;
        private bool _loadResources = false;
        private ObservableCollection<string> _raceCollection = new ObservableCollection<string>();
        private ObservableCollection<string> _classCollection = new ObservableCollection<string>();

        public SystemSettingsWindow()
        {
            
            _systemSettingManager = new SystemSettings(this);

            MainWindow.CurrentSettings = _systemSettingManager.SystemSetting;

            InitializeComponent();

            cbClassList.ItemsSource = _classCollection;
            cbRaceList.ItemsSource = _raceCollection;

        }

        private void LoadPlayerDetails()
        {
            foreach (ResourceTypes.RaceType race in GameResources.RaceList)
            {
                _raceCollection.Add(race.RaceName);
            }
            foreach(ResourceTypes.ClassType xClass in GameResources.ClassList)
            {
                _classCollection.Add(xClass.ClassName);
            }
        }

        private void LoadSystemSettings()
        {

            nbDisplayHeight.Value = _systemSettingManager.SystemSetting.DisplayHeight;
            nbDisplayWidth.Value = _systemSettingManager.SystemSetting.DisplayWidth;
            txtBBSAddress.Text = _systemSettingManager.SystemSetting.BBSAddress;
            txtBBSName.Text = _systemSettingManager.SystemSetting.BBSName;
            txtBBSPort.Text = _systemSettingManager.SystemSetting.BBSPort.ToString();
            txtDatabaseFile.Text = _systemSettingManager.SystemSetting.DatabaseFileName;
            MainWindow.SelectedDisplay.PaintFont = _systemSettingManager.SystemSetting.PaintFont;
            txtUsernameTrigger.Text = _systemSettingManager.SystemSetting.UsernameTrigger.Start;
            txtUserName.Text = _systemSettingManager.SystemSetting.BBSUserName;
            txtPassword.Text = _systemSettingManager.SystemSetting.BBSPassword;
            txtPasswordTrigger.Text = _systemSettingManager.SystemSetting.PasswordTrigger.Start;

            this.Title = "Profile - " + _systemSettingManager.SystemSetting.BBSName;

            MainWindow.CurrentSettings = _systemSettingManager.SystemSetting;

            LoadResources();

            cbClassList.SelectedIndex = _systemSettingManager.SystemSetting.ClassName;
            cbRaceList.SelectedIndex = _systemSettingManager.SystemSetting.RaceName;


        }

        private void NewProfile(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.InitialDirectory = @"C:\Yottamud Client\Profiles\";
            sfd.Filter = "Configuration File|*.xml";
            sfd.Title = "Save Configuration File";

            sfd.ShowDialog();

            if (sfd.FileName != "")
            {
                _systemSettingManager.NewSystemSettings();
                _systemSettingFileName = sfd.FileName;
            }
        }

        private void LoadProfile(object sender, RoutedEventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = @"C:\Yottamud Client\Profiles\";
            ofd.Filter = "Configuration File|*.xml";
            ofd.Title = "Save Configuration File";

            ofd.ShowDialog();

            if (ofd.FileName != "")
            {
                _systemSettingManager.LoadSystemSettings(ofd.FileName);
                _systemSettingFileName = ofd.FileName;
                LoadSystemSettings();
            }
        }

        private void btnLoadDBFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.DefaultExt = ".mdb";
            ofd.Filter = "Database File (.mdb)|*.mdb";
            ofd.ShowDialog();

            if (ofd.FileName != "")
            {
                txtDatabaseFile.Text = ofd.FileName;
                _systemSettingManager.SystemSetting.DatabaseFileName = ofd.FileName;
            }
        }

        private void txtDatabaseFile_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDatabaseFile.Text.Length > 0)
            {
                _systemSettingManager.SystemSetting.DatabaseFileName = txtDatabaseFile.Text;
            }
        }

        private void UpdateSystemSettings()
        {

            _systemSettingManager.SystemSetting.DatabaseFileName = txtDatabaseFile.Text;
            _systemSettingManager.SystemSetting.BBSAddress = txtBBSAddress.Text;

                if (txtBBSPort.Text == "")
                {
                    txtBBSPort.Text = "0";
                }
                
                _systemSettingManager.SystemSetting.DisplayHeight = (int)nbDisplayHeight.Value;
                _systemSettingManager.SystemSetting.DisplayWidth = (int)nbDisplayWidth.Value;
                _systemSettingManager.SystemSetting.BBSPort = int.Parse(txtBBSPort.Text);
                _systemSettingManager.SystemSetting.BBSName = txtBBSName.Text;
                _systemSettingManager.SystemSetting.UsernameTrigger = new GameDataMonitor.GameTrigger { Start = txtUsernameTrigger.Text,
                                                                                                        MiscData = txtUserName.Text,
                                                                                                        Type = GameDataMonitor.GameInformation.LoginUser};
                _systemSettingManager.SystemSetting.DisplayFontName = _systemSettingManager.SystemSetting.PaintFont.Name;
                _systemSettingManager.SystemSetting.DisplayFontSize = _systemSettingManager.SystemSetting.PaintFont.Size;
                _systemSettingManager.SystemSetting.DisplayFontStyle = _systemSettingManager.SystemSetting.PaintFont.Style;
                _systemSettingManager.SystemSetting.BBSUserName = txtUserName.Text;
                _systemSettingManager.SystemSetting.BBSPassword = txtPassword.Text;

                _systemSettingManager.SystemSetting.PasswordTrigger = new GameDataMonitor.GameTrigger
                {
                    Start = txtPasswordTrigger.Text,
                    MiscData = txtPassword.Text,
                    Type = GameDataMonitor.GameInformation.LoginPassword
                };

                MainWindow.SelectedDisplay.PaintFont = _systemSettingManager.SystemSetting.PaintFont;
            
                _systemSettingManager.SystemSetting.ClassName = cbClassList.SelectedIndex;
                _systemSettingManager.SystemSetting.RaceName = cbRaceList.SelectedIndex;
                MainWindow.CurrentSettings = _systemSettingManager.SystemSetting;

                ResizeDisplay();
              
        }

        private void btnSaveSettings_Click(object sender, RoutedEventArgs e)
        {
            UpdateSystemSettings();

            _systemSettingManager.SaveSystemSettings(_systemSettingFileName);

            LoadResources();

         
        }

        private void txtBBSPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                int port = int.Parse(txtBBSPort.Text);

                if (port >= 0 && port <= 65535)
                {
                    txtBBSPort.Text = port.ToString();
                }
                else
                {
                    MessageBox.Show("Out of range Port");
                    txtBBSPort.Text = "";
                }
            }
            catch { txtBBSPort.Text = ""; }
        }

        private void ResizeDisplay()
        {
            MainWindow.SelectedDisplay.GetFontInfo();

            MainWindow.SelectedDisplay.SetSize(_systemSettingManager.SystemSetting.DisplayHeight, 
                                          _systemSettingManager.SystemSetting.DisplayWidth);

           
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        private void btnFontSelector_Click(object sender, RoutedEventArgs e)
        {

            System.Windows.Forms.FontDialog fdSelect = new System.Windows.Forms.FontDialog();

            if (MainWindow.SelectedDisplay.PaintFont != null)
            {
                fdSelect.Font = MainWindow.SelectedDisplay.PaintFont;
            }

            fdSelect.FixedPitchOnly = true;
            fdSelect.ShowEffects = false;

            if (fdSelect.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                _systemSettingManager.SystemSetting.PaintFont = fdSelect.Font;
            }

        }

        private void LoadResources(object sender, RoutedEventArgs e)
        {
            LoadResources();
        }

        private void LoadResources()
        {
            if (!_loadResources)
            {
                _loadResources =  MainWindow.LoadResources();
                LoadPlayerDetails();

            }

        }

        private void nbResumeHP_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
           // UpdatePlayerHealthPercentSetting();
        }

        private void nbRestHP_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //UpdatePlayerHealthPercentSetting();
        }

        private void nbRestHealHP_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
          //  UpdatePlayerHealthPercentSetting();
        }

        private void nbRestCombatHP_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
           // UpdatePlayerHealthPercentSetting();
        }

        private void nbHangupHP_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //UpdatePlayerHealthPercentSetting();
        }

        private void txtUsernameTrigger_TextChanged(object sender, TextChangedEventArgs e)
        {
            _systemSettingManager.SystemSetting.UsernameTrigger = new GameDataMonitor.GameTrigger { Start = txtUsernameTrigger.Text, 
                                                                                  MiscData = txtUserName.Text, Type = GameDataMonitor.GameInformation.LoginUser };
        }

        private void txtPasswordTrigger_TextChanged(object sender, TextChangedEventArgs e)
        {
            _systemSettingManager.SystemSetting.PasswordTrigger = new GameDataMonitor.GameTrigger
            {
                Start = txtPasswordTrigger.Text,
                MiscData = txtPassword.Text,
                Type = GameDataMonitor.GameInformation.LoginPassword
            };
        }

   

    
    }
}
